#!/usr/bin/env python3.9

COLSTEP=1
COLOURSPACE="RGB" # or "YCbCr"
CARDSIZE=(320,256)
(xmax, ymax) = CARDSIZE

background = 255
foreground = 255-background

from PIL import Image, ImageDraw, ImageFont
fnt = ImageFont.truetype("/usr/home/cjb/.fonts/PetMe64.ttf", 16)

im = Image.new(mode=COLOURSPACE, size=CARDSIZE, color=(background,background,background))
d = ImageDraw.Draw(im)

def docheck (xs, ys, col):
  global si, d
  s = si // 4
  y = int(ys) -1
  while (y-ys) <= si:
    x = int(xs) -1
    cp = (int(y - ys) // s) & 1
    while (x-xs) <= si:
      c = (int(x - xs) // s) & 1
      if (c ^ cp):
        #print (c^cp, si, x, y, col)
        im.putpixel ((x, y), (col,col,col))
      x += 1
    y +=1
  d.text ((xs,y), str(col), font=fnt, fill=(128,128,128,255))

gx = xmax // 16
gy = ymax // 16
si = min (gx, gy) * 4

col = background
if background > foreground:
  colstep = COLSTEP
  s="overexposuretest-"
else:
  s="underexposuretest-"
  colstep = -COLSTEP
yp = gy
while (yp <= (12*gy)):
  xp = gx
  while (xp <= (12*gx)):
    col -= colstep
    docheck (xp, yp, col)
    xp += 3.5*gx
  yp += 5*gy

d.text ((xmax/2,4), "VK2CJB", font=fnt, fill=(foreground,foreground,foreground,255))
imrgb = im.convert("RGB")
s += str(xmax) + "x" + str(ymax) + ".png"
print (s)
imrgb.save(s)
